package com.java.zhuiyun.api.achievement.service.impl;

import com.java.zhuiyun.api.achievement.dao.AchievementMapper;
import com.java.zhuiyun.api.achievement.entity.Achievement;
import com.java.zhuiyun.api.achievement.service.AchievementService;
import com.java.zhuiyun.api.image_file.service.ImageFileService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AchievementServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/9/13 8:57
 **/
@Service
public class AchievementServiceImpl implements AchievementService {
    @Resource
    AchievementMapper achievementMapper;

    @Resource
    ImageFileService imageFileService;
    /**
    * @Author MJL
    * @Description todo 添加成就信息
    * @Date 2023/9/13 10:59
    * @Return  * @param null
    */
    @Override
    public CommonResult addAchievement(Achievement achievement, MultipartFile file) {
        // 判断是否有图片
        if (file != null) {
            // 将图片上传至服务器
            String addImageFile = imageFileService.addImageFile(file);
            // 判断上传成功的图片是否为空
            if (addImageFile != null) {
                achievement.setAchievementImgId(Integer.valueOf(addImageFile));
            }
        }

        // 获取添加时间
        achievement.setCreateTime(new Date());
        // 添加时默认为禁用状态
        achievement.setAchievementStatus(1);
        // 添加时删除标识默认为0
        achievement.setDelFlag(0);
        // 添加方法
        Integer a = achievementMapper.addAchievement(achievement);
        if (a > 0) {
            return CommonResult.ok("添加成功");
        } else {
            return CommonResult.error("添加失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 查看全部成就信息
    * @Date 2023/9/13 10:59
    * @Return  * @param null
    */
    @Override
    public PageResult queryAchievement(Achievement achievement, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(achievementMapper.queryAchievement(achievement));
        return pageResult;
    }
    /**
    * @Author MJL
    * @Description todo 修改成就信息
    * @Date 2023/9/13 14:15
    * @Return  * @param null
    */
    @Transactional
    @Override
    public CommonResult updateAchievement(Achievement achievement, MultipartFile file) {
        // 将图片上传至服务器
        String i= UploadFileUtils.uploadFile(file);
        if (i == "") {
            return CommonResult.ok("图片上传失败");
        }
        // 获取上传的图片id
        achievement.setAchievementImgId(Integer.valueOf(i));
        // 获取修改时间
        achievement.setUpdateTime(new Date());
        Integer a = achievementMapper.updateAchievement(achievement);
        if (a > 0){
            return CommonResult.ok("修改成功");
        } else {
            return CommonResult.error("修改失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 删除单个成就
    * @Date 2023/9/13 14:24
    * @Return  * @param null
    */
    @Override
    public CommonResult deleteAchievement(Integer id) {
        Integer i = achievementMapper.deleteAchievement(id);
        if (i > 0) {
            return CommonResult.ok("操作成功");
        } else {
            return CommonResult.error("操作失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 将成就下架
    * @Date 2023/9/13 14:41
    * @Return  * @param null
    */
    @Override
    public CommonResult bannedAchievement(Integer id) {
        Integer i = achievementMapper.bannedAchievement(id);
        if (i > 0) {
            return CommonResult.ok("操作成功");
        } else {
            return CommonResult.error("操作失败");
        }
    }
    /**
    * @Author MJL
    * @Description todo 将成就上架
    * @Date 2023/9/13 14:41
    * @Return  * @param null
    */
    @Override
    public CommonResult enableAchievement(Integer id) {
        Integer i = achievementMapper.enableAchievement(id);
        if (i > 0) {
            return CommonResult.ok("操作成功");
        } else {
            return CommonResult.error("操作失败");
        }
    }

    @Override
    public CommonResult getByIdIdiom(Integer id) {
        return achievementMapper.getByIdIdiom(id);
    }

    /*
     * @Author WX
     * @Description 查询可用成就信息
     * @Date 2023/10/16 8:52
     * @Return
     * @param null
     */
    @Override
    public CommonResult selectAvailableAchievement() {
        List<Achievement> achievement = achievementMapper.selectAvailableAchievement();
        return CommonResult.ok(achievement);
    }
}
